#include <vector>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <bitset>
using namespace std;

// find C(length(str), N)
vector<string> docombine(string str,unsigned N){
	vector<string> results;
	if(str.size()<N)
		return results;
	if(N==1){
		const char* s = str.c_str();
		while(*s)
			results.push_back(string(1,*s++));
		return results;
	}
	const char* s = str.c_str();
	while(*s){
		string str_r(1,*s++);
		string str_left(s);
		vector<string> more = docombine(str_left,N-1);
		for(vector<string>::iterator i = more.begin(); i!=more.end(); ++i){
			results.push_back(str_r+*i);
		}
	}
	return results;
}

int main(){
	vector<string> results = docombine("abcd",3);
    for(vector<string>::iterator i = results.begin(); i!=results.end(); ++i){
		cout <<*i <<endl;
	}
}
